var actbill = require('../../xc/model/diy_index')
var actbillpro = require('../../xc/model/pro_contents')
var syscart = require('../../xc/sys/cart')
Page({
  mixins: [require('../../xc/mixins/basicMixin')],
  /**
   * 页面的初始数据
   */
  data: {
    memberinfo: null,
    locationaddr: null,
    locationaddrtxt: "定位中...",
    diydata: null,
    themcolor:null,
    datasourcepara: {}, 
    pageloading: true,
    groupid: "",
    StatusBar: "",
    istop: false,
    xcpagesys: {},
    diypara: {  //团长长参数
      clubid: 0,  //团长
      diyid: 0,
    },
    cachecartinfo: {
      // cart:{},
      // groupcarnum:{pro:{}},
      // totalnum:

    },
    totalnuminfo: {},
    // groupcarnum:{
    //   pro:{},
    // }
  },

 
  /**
   * 生命周期函数--监听页面加载
   */
  changelocation(ev) {
    var topnav = this.selectComponent("#topnav");
    if (topnav) {
      topnav.updatedata()
    }
  },
  afterlogin(ev, config, userinfo) {
    if (userinfo["memberinfo"]) {
      this.setData({ memberinfo: userinfo["memberinfo"] })
    }
  },
  navclick(ev) {
    var locationseachaddr = this.selectComponent("#locationseachaddr");
    if (locationseachaddr) {
      locationseachaddr.show();
    }
  },
  cartop(ev) {
    // var opmodel={item:pro,op:"add",num:1}
    var detail = ev.detail
    var that = this;
    switch (detail.op) {
      case "reload": {
        that.loadcartinfo();
        //
        break;
      }
    }
  },
  buy(ev) {
    var that = this
    var dataset = ev.detail.dataset
    var item = dataset.item
    var option = {};
    var optionfileds = ["atype", "aid"];
    optionfileds.forEach(atrrkey => {
      if (dataset.hasOwnProperty(atrrkey)) {
        option[atrrkey] = dataset[atrrkey]
      }
    })
    // 1加购 2立即购买 3两种
    actbillpro.showattrchose(that, item.id, null, 3, option)
  },
  //
  gettop(topset) {
    var app = getApp();
    if (topset == "1") {
      //标签距离
      return (app.globalData.CustomBar - 2)
    } else if (topset == "2") {
      //到顶部的距离
      return app.globalData.Custom.top;
    }
    return 0;
  },
   
  //特殊处理定位
  secuposit(obj) {
    var that = this;
    if (obj && obj["css"]) {
      if (obj["csscuposit"]) {
        var csscuposit = obj["csscuposit"]
        var strcsscuposit = []
        strcsscuposit.push("position:" + csscuposit["position"])
        strcsscuposit.push("z-index:" + csscuposit["z-index"])
        var allowfield = ["top", "left", "right", "bottom"];
        var sizeunit = csscuposit["pos"].sizeunit;
        var pos = csscuposit["pos"]
        allowfield.forEach(item => {
          if (pos[item]) {
            if (item == "top") {
              var xval = that.gettop(csscuposit["topset"])
              if (xval) {
                strcsscuposit.push(item + ":" + "calc(" + xval + "px + " + pos[item].val + sizeunit + ")")
              }

            } else {
              strcsscuposit.push(item + ":" + pos[item].val + sizeunit)
            }
          }
        })
        return ";" + strcsscuposit.join(";");
      }
    }
    return ""
  },

  loadpagesys() {
    var that = this;
    //xcpagesys
    setTimeout(() => {
      wx.createSelectorQuery().selectAll('#footer').boundingClientRect(function (rect) {
        var temupdate = {};
        temupdate["xcpagesys.foot"] = rect[0];
        that.setData(temupdate);
      }).exec()

    }, 500);
  },

  setpage(selector) {

    wx.pageScrollTo({
      selector: ".diytabprocom",
      duration: 300,
    }
    )
  },
  xcloaddata: function (options, config) {
    var app = getApp();
    var updatemodelfirst = { options };
    if (config.prothem && config.prothem.popup) {
      updatemodelfirst["themcolor"] =config.prothem.config
      updatemodelfirst["popconfig"] = config.prothem.popup.proconfig
      updatemodelfirst["popCustomStyle"] = config.prothem.popup.customStyle      
    }
    var diypara = this.data.diypara
    var diyparafield = ["clubid", "diyid"]
    diyparafield.forEach(function (field) {
      if (options[field]) {
        diypara[field] = options[field];
      }
    })
    

   

    updatemodelfirst["StatusBar"] = app.globalData.StatusBar
    updatemodelfirst["CustomBar"] = app.globalData.CustomBar
    updatemodelfirst["diypara"] = diypara;
    //diypara
    this.setData(updatemodelfirst)
    var diyid = 0;
    if (options && options.diyid) {

      diyid = options.diyid
    }
    var height = wx.getSystemInfoSync().windowHeight
    var that = this;

    actbill.diydata(diyid).then(function (reslutdata) {

      if (reslutdata["status"] == 1) {
        var nobj = reslutdata["obj"];

        diypara["diyid"] = nobj.id
        var upatemodel = {};
        upatemodel["diydata"] = JSON.parse(nobj["compilecontents"])
        var body = upatemodel["diydata"]["body"]
     
       

        if (body) {
          for (var a in body) {
            body[a]["strcsscuposit"] = that.secuposit(body[a]);           

        
          }
        }
        upatemodel["datasourcepara"] = nobj["datasourcepara"]
        upatemodel["pageloading"] = false

        upatemodel["diypara"] = diypara;
        that.setData(upatemodel)
        that.loadpagesys();
        wx.createIntersectionObserver().relativeToViewport({ bottom: -(height - 50) }).observe('.diytabprocom', (res) => {
          if (res.intersectionRatio == 0) {
            that.setData({ istop: false })
          }
          else {    
            that.setData({ istop: true })
          }
        })

      }
    })


    this.loadcartinfo()
  },
  onLoad: function (options) {
    // wx.redirectTo({
    //   url: '/xc_lshop/club/manage/pro/list',
    // })
    // this.locationaddrfunc();
  },
  onClickLeft() {
    wx.showToast({ title: '点击返回', icon: 'none' });
  },
  onClickRight() {
    wx.showToast({ title: '点击按钮', icon: 'none' });
  },
  loadcartinfo() {
    var that = this
    syscart.loadcartlocal().then(function name(resobj) {
      that.setData({ cachecartinfo: resobj, totalnuminfo: resobj["totalnuminfo"] })
    })
  },
  cartclosed() {
    var that = this
    that.loadcartinfo()
  },

  cartsumbit(){
    // 提交购买车订单
    var app =getApp();
   var chiefcomv= this.selectComponent("#chiefcomv")
   if(chiefcomv){
   var addrinfo= chiefcomv.getaddrinfo();

   app.globalData.temcart["addrinfo"] = addrinfo

   console.log("addrinfoaddrinfo---addrinfoaddrinfo")
   console.log(addrinfo)
  
   }
   else
   {
    app.globalData.temcart["addrinfo"]=null
   }

  
 
  
 
    wx.navigateTo({
      url: '/' + app.globalData.xcsys.m + '/shop/paycore/payinfo?iscart=1&hasaddr=1'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  backtop(){
    wx.pageScrollTo({
      scrollTop: 0
    })
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var diytabprocom = this.selectComponent(".diytabprocom")
    if (diytabprocom) {
      diytabprocom.pagebottom()
    }


  },


})